package com.lagou.controller;

import com.lagou.domain.Menu;
import com.lagou.domain.ResponseResult;
import com.lagou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有菜单信息
     * @return
     */
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        List<Menu> list = menuService.findAlleMemu();
        return new ResponseResult(true,200,"查找成功",list);
    }

    /**
     * 添加 & 修改菜单信息
     * @param id
     * @return
     */
    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(Integer id) {
        if (id == -1) {
            //添加操作
            List<Menu> list = menuService.findSubMenuListByPid(-1);
            Map<String, Object> map = new HashMap<>();
            map.put("menuList",null);
            map.put("parentMenuList", list);
            return new ResponseResult(true,200,"添加回显成功",map);
        } else {
            Menu menu = menuService.findMenuById(id);
            List<Menu> list = menuService.findSubMenuListByPid(-1);
            Map<String, Object> map = new HashMap<>();
            map.put("menuList",menu);
            map.put("parentMenuList", list);
            return new ResponseResult(true,200,"修改回显成功",map);
        }
    }
}
